﻿using Unknown.Framework.General;
using Unknown.Source.Entity.NormalView;

namespace Unknown.Source.Business.Coder
{
    /// <summary>
    /// Oracle数据类型处理类
    /// </summary>
    public class OracleDataTypeHandler : IDataTypeHandler
    {
        /// <summary>
        /// 构建数据类型
        /// </summary>
        /// <param name="column">字段</param>
        /// <returns>结果</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        public string BuildDataType(Column column)
        {
            string result = null;

            if (column != null)
            {
                string dataTypeLower = column.DataType.ToLower(Variable.Culture);

                switch (dataTypeLower)
                {
                    case "blob":
                        result = "byte[]";
                        break;
                    case "clob":
                        result = "string";
                        break;
                    case "date":
                        if (column.Nullable)
                        {
                            result = "DateTime?";
                        }
                        else
                        {
                            result = "DateTime";
                        }
                        break;
                    case "nclob":
                        result = "string";
                        break;
                    case "number":
                        if (column.Nullable)
                        {
                            if (column.Scale > 0)
                            {
                                result = "decimal?";
                            }
                            else
                            {
                                if (column.Precision > 10)
                                {
                                    result = "long?";
                                }
                                else
                                {
                                    result = "int?";
                                }
                            }
                        }
                        else
                        {
                            if (column.Scale > 0)
                            {
                                result = "decimal";
                            }
                            else
                            {
                                if (column.Precision > 10)
                                {
                                    result = "long";
                                }
                                else
                                {
                                    result = "int";
                                }
                            }
                        }
                        break;
                    case "nvarchar2":
                        result = "string";
                        break;
                    case "timestamp(6)":
                        if (column.Nullable)
                        {
                            result = "DateTime?";
                        }
                        else
                        {
                            result = "DateTime";
                        }
                        break;
                    case "varchar2":
                        result = "string";
                        break;
                }
            }

            return result;
        }
        
        /// <summary>
        /// 构建赋值方法
        /// </summary>
        /// <param name="column">字段</param>
        /// <returns>结果</returns>
        public string BuildAssign(Column column)
        {
            string result = null;

            if (column != null)
            {
                string dataTypeLower = column.DataType.ToLower(Variable.Culture);

                switch (dataTypeLower)
                {
                    case "blob":
                        result = "AssignBinary";
                        break;
                    case "clob":
                        result = "AssignString";
                        break;
                    case "date":
                        result = "AssignTime";
                        break;
                    case "nclob":
                        result = "AssignString";
                        break;
                    case "number":
                        if (column.Scale > 0)
                        {
                            result = "AssignDecimal";
                        }
                        else
                        {
                            if (column.Precision > 10)
                            {
                                result = "AssignNumber";
                            }
                            else
                            {
                                result = "AssignInteger";
                            }
                        }
                        break;
                    case "nvarchar2":
                        result = "AssignString";
                        break;
                    case "timestamp(6)":
                        result = "AssignTime";
                        break;
                    case "varchar2":
                        result = "AssignString";
                        break;
                }
            }

            return result;
        }

        /// <summary>
        /// 构建默认值
        /// </summary>
        /// <param name="column">字段</param>
        /// <returns>结果</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        public string BuildDefaultValue(Column column)
        {
            string result = null;

            if (column != null)
            {
                string dataTypeLower = column.DataType.ToLower(Variable.Culture);

                switch (dataTypeLower)
                {
                    case "blob":
                        result = "null";
                        break;
                    case "clob":
                        result = "null";
                        break;
                    case "date":
                        if (column.Nullable)
                        {
                            result = "null";
                        }
                        else
                        {
                            result = "DateTime.MinValue";
                        }
                        break;
                    case "nclob":
                        result = "null";
                        break;
                    case "number":
                        if (column.Nullable)
                        {
                            if (column.Scale > 0)
                            {
                                result = "null";
                            }
                            else
                            {
                                if (column.Precision > 10)
                                {
                                    result = "null";
                                }
                                else
                                {
                                    result = "null";
                                }
                            }
                        }
                        else
                        {
                            if (column.Scale > 0)
                            {
                                result = "0";
                            }
                            else
                            {
                                if (column.Precision > 10)
                                {
                                    result = "0";
                                }
                                else
                                {
                                    result = "0";
                                }
                            }
                        }
                        break;
                    case "nvarchar2":
                        result = "null";
                        break;
                    case "timestamp(6)":
                        if (column.Nullable)
                        {
                            result = "null";
                        }
                        else
                        {
                            result = "DateTime.MinValue";
                        }
                        break;
                    case "varchar2":
                        result = "null";
                        break;
                }
            }

            return result;
        }

        /// <summary>
        /// 构建数据库类型
        /// </summary>
        /// <param name="column">字段</param>
        /// <returns>结果</returns>
        public string BuildDatabaseType(Column column)
        {
            string result = null;

            if (column != null)
            {
                string dataTypeLower = column.DataType.ToLower(Variable.Culture);

                switch (dataTypeLower)
                {
                    case "blob":
                        result = "Blob";
                        break;
                    case "clob":
                        result = "Clob";
                        break;
                    case "date":
                        result = "Date";
                        break;
                    case "nclob":
                        result = "NClob";
                        break;
                    case "number":
                        if (column.Scale > 0)
                        {
                            result = "Decimal";
                        }
                        else
                        {
                            if (column.Precision > 10)
                            {
                                result = "Int64";
                            }
                            else
                            {
                                result = "Int32";
                            }
                        }
                        break;
                    case "nvarchar2":
                        result = "NVarchar2";
                        break;
                    case "timestamp(6)":
                        result = "TimeStamp";
                        break;
                    case "varchar2":
                        result = "Varchar2";
                        break;
                }
            }

            return result;
        }
    }
}
